package com.ruoyi.common.excel.converter;

import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.annotation.ExcelConverter;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.model.InnerObj;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.apache.ibatis.javassist.runtime.Inner;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.LinkedCaseInsensitiveMap;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 最爱喝兽奶
 * @create 2022/9/9
 **/
public class ObjectFieldConverter implements Converter {

    private RedisCache redisCache = SpringUtils.getBean(RedisCache.class);

    private JdbcTemplate jdbcTemplate = SpringUtils.getBean(JdbcTemplate.class);

    private static final String prefix = "EXCEL:CONVERTER:";

    @Override
    public Object cellToVal(Field field, Excel excel, Object label) {
        if (field.isAnnotationPresent(ExcelConverter.class)) {
            ExcelConverter excelConverter = field.getAnnotation(ExcelConverter.class);
            // 被关联的表
            String refTable = excelConverter.refTable();
            // 被关联的字段
            String refField = excelConverter.refField();
            // 用于展示的字段
            String showField = excelConverter.showField();
            // 用于保存于redis的键
            String key = excelConverter.key();
            if (excelConverter.isMultiply()) {
                // 如果是多值对象
                StringBuilder sb = new StringBuilder();
                if (label instanceof String) {
                    String str = (String)label;
                    String[] split = str.split(excelConverter.separator());
                    for (int i = 0; i < split.length; i++) {
                        Object val = converterValueForCache(split[i], excelConverter);
                        if (null == val) {
                            continue;
                        }
                        sb.append(val);
                        if (i != split.length - 1) {
                            sb.append(",");
                        }
                    }
                    return sb.toString();
                }
            }else {
                // 不是多值对象的处理方式
                return converterValueForCache(label, excelConverter);
            }
        }
        return null;
    }

    private Object converterValueForCache(Object label, ExcelConverter converter) {
        List<Map<String, Object>> cacheList = redisCache.getCacheList(prefix + converter.key());
        String refField = converter.refField();
        String showField = converter.showField();
        String refTable = converter.refTable();
        String key = converter.key();
        if (null != cacheList && cacheList.size() > 0) {
            return converterValue(cacheList, label);
        } else {
            // 如果不存在于缓存中，就查询数据库
            String sql = String.format("select %s refField, %s showField from %s", refField, showField, refTable);
            List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
            redisCache.setCacheList(prefix + key, maps);
            for (Map<String, Object> map : maps) {
                if (map.get("showField").equals(label)) {
                    return map.get("refField");
                }
            }
        }
        return null;
    }

    private Object converterValue(List<Map<String, Object>> list, Object label) {
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> innerObj = list.get(i);
            if (label.equals(innerObj.get("showField"))) {
                return innerObj.get("refField");
            }
        }
        return null;
    }
}
